/*
 * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#include "mbedtls/platform.h"
#include "mbedtls_rom_osi.h"

void mbedtls_rom_osi_functions_init(void);

static void mbedtls_rom_mutex_init( mbedtls_threading_mutex_t *mutex )
{
#if (!defined(CONFIG_MBEDTLS_THREADING_C))
    ((void) mutex);
    return;
#else
    mbedtls_mutex_init(mutex);
#endif
}

static void mbedtls_rom_mutex_free( mbedtls_threading_mutex_t *mutex )
{
#if (!defined(CONFIG_MBEDTLS_THREADING_C))
    ((void) mutex);
    return;
#else
    mbedtls_mutex_free(mutex);
#endif
}

static int mbedtls_rom_mutex_lock( mbedtls_threading_mutex_t *mutex )
{
#if (!defined(CONFIG_MBEDTLS_THREADING_C))
    ((void) mutex);
    return 0;
#else
    return mbedtls_mutex_lock(mutex);
#endif
}

static int mbedtls_rom_mutex_unlock( mbedtls_threading_mutex_t *mutex )
{
#if (!defined(CONFIG_MBEDTLS_THREADING_C))
    ((void) mutex);
    return 0;
#else
    return mbedtls_mutex_unlock(mutex);
#endif
}

/* This structure can be automatically generated by the script with rom.mbedtls.ld. */
static const mbedtls_rom_funcs_t mbedtls_rom_funcs_table = {
    /* Fill the ROM functions into mbedtls rom function table. */
    /* aes module */
    ._rom_mbedtls_aes_init = mbedtls_aes_init,
    ._rom_mbedtls_aes_free = mbedtls_aes_free,
    ._rom_mbedtls_aes_setkey_enc = mbedtls_aes_setkey_enc,
    ._rom_mbedtls_aes_setkey_dec = mbedtls_aes_setkey_dec,
    ._rom_mbedtls_aes_crypt_ecb = mbedtls_aes_crypt_ecb,
    ._rom_mbedtls_aes_crypt_cbc = mbedtls_aes_crypt_cbc,
    ._rom_mbedtls_internal_aes_encrypt = mbedtls_internal_aes_encrypt,
    ._rom_mbedtls_internal_aes_decrypt = mbedtls_internal_aes_decrypt,
    /* asn1 module */
    ._rom_mbedtls_asn1_get_len = mbedtls_asn1_get_len,
    ._rom_mbedtls_asn1_get_tag = mbedtls_asn1_get_tag,
    ._rom_mbedtls_asn1_get_bool = mbedtls_asn1_get_bool,
    ._rom_mbedtls_asn1_get_int = mbedtls_asn1_get_int,
    ._rom_mbedtls_asn1_get_bitstring = mbedtls_asn1_get_bitstring,
    ._rom_mbedtls_asn1_get_bitstring_null = mbedtls_asn1_get_bitstring_null,
    ._rom_mbedtls_asn1_get_sequence_of = mbedtls_asn1_get_sequence_of,
    ._rom_mbedtls_asn1_get_mpi = mbedtls_asn1_get_mpi,
    ._rom_mbedtls_asn1_get_alg = mbedtls_asn1_get_alg,
    ._rom_mbedtls_asn1_get_alg_null = mbedtls_asn1_get_alg_null,
    ._rom_mbedtls_asn1_write_len = mbedtls_asn1_write_len,
    ._rom_mbedtls_asn1_write_tag = mbedtls_asn1_write_tag,
    ._rom_mbedtls_asn1_write_mpi = mbedtls_asn1_write_mpi,
    /* base64 moudle */
    ._rom_mbedtls_base64_decode = mbedtls_base64_decode,
    /* bignum module */
    ._rom_mbedtls_mpi_init = mbedtls_mpi_init,
    ._rom_mbedtls_mpi_free = mbedtls_mpi_free,
    ._rom_mbedtls_mpi_grow = mbedtls_mpi_grow,
    ._rom_mbedtls_mpi_shrink = mbedtls_mpi_shrink,
    ._rom_mbedtls_mpi_copy = mbedtls_mpi_copy,
    ._rom_mbedtls_mpi_safe_cond_assign = mbedtls_mpi_safe_cond_assign,
    ._rom_mbedtls_mpi_safe_cond_swap = mbedtls_mpi_safe_cond_swap,
    ._rom_mbedtls_mpi_lset = mbedtls_mpi_lset,
    ._rom_mbedtls_mpi_get_bit = mbedtls_mpi_get_bit,
    ._rom_mbedtls_mpi_set_bit = mbedtls_mpi_set_bit,
    ._rom_mbedtls_mpi_lsb = mbedtls_mpi_lsb,
    ._rom_mbedtls_mpi_bitlen = mbedtls_mpi_bitlen,
    ._rom_mbedtls_mpi_size = mbedtls_mpi_size,
    ._rom_mbedtls_mpi_read_binary = mbedtls_mpi_read_binary,
    ._rom_mbedtls_mpi_write_binary = mbedtls_mpi_write_binary,
    ._rom_mbedtls_mpi_shift_l = mbedtls_mpi_shift_l,
    ._rom_mbedtls_mpi_shift_r = mbedtls_mpi_shift_r,
    ._rom_mbedtls_mpi_cmp_abs = mbedtls_mpi_cmp_abs,
    ._rom_mbedtls_mpi_cmp_mpi = mbedtls_mpi_cmp_mpi,
    ._rom_mbedtls_mpi_lt_mpi_ct = mbedtls_mpi_lt_mpi_ct,
    ._rom_mbedtls_mpi_cmp_int = mbedtls_mpi_cmp_int,
    ._rom_mbedtls_mpi_add_abs = mbedtls_mpi_add_abs,
    ._rom_mbedtls_mpi_sub_abs = mbedtls_mpi_sub_abs,
    ._rom_mbedtls_mpi_add_mpi = mbedtls_mpi_add_mpi,
    ._rom_mbedtls_mpi_sub_mpi = mbedtls_mpi_sub_mpi,
    ._rom_mbedtls_mpi_add_int = mbedtls_mpi_add_int,
    ._rom_mbedtls_mpi_sub_int = mbedtls_mpi_sub_int,
    ._rom_mbedtls_mpi_mul_mpi = mbedtls_mpi_mul_mpi,
    ._rom_mbedtls_mpi_mul_int = mbedtls_mpi_mul_int,
    ._rom_mbedtls_mpi_div_mpi = mbedtls_mpi_div_mpi,
    ._rom_mbedtls_mpi_div_int = mbedtls_mpi_div_int,
    ._rom_mbedtls_mpi_mod_mpi = mbedtls_mpi_mod_mpi,
    ._rom_mbedtls_mpi_mod_int = mbedtls_mpi_mod_int,
    ._rom_mbedtls_mpi_exp_mod = mbedtls_mpi_exp_mod,
    ._rom_mbedtls_mpi_fill_random = mbedtls_mpi_fill_random,
    ._rom_mbedtls_mpi_gcd = mbedtls_mpi_gcd,
    ._rom_mbedtls_mpi_inv_mod = mbedtls_mpi_inv_mod,
    ._rom_mbedtls_mpi_is_prime_ext = mbedtls_mpi_is_prime_ext,
    /* ccm module */
    ._rom_mbedtls_ccm_star_encrypt_and_tag = mbedtls_ccm_star_encrypt_and_tag,
    ._rom_mbedtls_ccm_star_auth_decrypt = mbedtls_ccm_star_auth_decrypt,
    /* cipher module */
    ._rom_mbedtls_cipher_init = mbedtls_cipher_init,
    ._rom_mbedtls_cipher_set_padding_mode = mbedtls_cipher_set_padding_mode,
    ._rom_mbedtls_cipher_reset = mbedtls_cipher_reset,
    ._rom_mbedtls_cipher_finish = mbedtls_cipher_finish,
    ._rom_mbedtls_cipher_crypt = mbedtls_cipher_crypt,
    ._rom_mbedtls_cipher_cmac_starts = mbedtls_cipher_cmac_starts,
    ._rom_mbedtls_cipher_cmac_update = mbedtls_cipher_cmac_update,
    ._rom_mbedtls_cipher_cmac_finish = mbedtls_cipher_cmac_finish,
    /* ctr drbg module */
    ._rom_mbedtls_ctr_drbg_init = mbedtls_ctr_drbg_init,
    ._rom_mbedtls_ctr_drbg_seed = mbedtls_ctr_drbg_seed,
    ._rom_mbedtls_ctr_drbg_free = mbedtls_ctr_drbg_free,
    ._rom_mbedtls_ctr_drbg_reseed = mbedtls_ctr_drbg_reseed,
    ._rom_mbedtls_ctr_drbg_random_with_add = mbedtls_ctr_drbg_random_with_add,
    ._rom_mbedtls_ctr_drbg_random = mbedtls_ctr_drbg_random,
    /* sha1 module */
    ._rom_mbedtls_sha1_init = mbedtls_sha1_init,
    ._rom_mbedtls_sha1_free = mbedtls_sha1_free,
    ._rom_mbedtls_sha1_clone = mbedtls_sha1_clone,
    ._rom_mbedtls_sha1_starts = mbedtls_sha1_starts,
    ._rom_mbedtls_sha1_finish = mbedtls_sha1_finish,
    /* sha256 module */
    ._rom_mbedtls_sha256_init = mbedtls_sha256_init,
    ._rom_mbedtls_sha256_free = mbedtls_sha256_free,
    ._rom_mbedtls_sha256_clone = mbedtls_sha256_clone,
    ._rom_mbedtls_sha256_starts = mbedtls_sha256_starts,
    ._rom_mbedtls_sha256_finish = mbedtls_sha256_finish,
    ._rom_mbedtls_sha256 = mbedtls_sha256,
    /* sha512 module */
    ._rom_mbedtls_sha512_init = mbedtls_sha512_init,
    ._rom_mbedtls_sha512_free = mbedtls_sha512_free,
    ._rom_mbedtls_sha512_clone = mbedtls_sha512_clone,
    ._rom_mbedtls_sha512_starts = mbedtls_sha512_starts,
    ._rom_mbedtls_sha512_update = mbedtls_sha512_update,
    ._rom_mbedtls_sha512_finish = mbedtls_sha512_finish,
    ._rom_mbedtls_internal_sha512_process = mbedtls_internal_sha512_process,
    ._rom_mbedtls_sha512 = mbedtls_sha512,

    /* Fill the platform functions into mbedtls rom function table. */
    ._mbedtls_mutex_init = mbedtls_rom_mutex_init,
    ._mbedtls_mutex_free = mbedtls_rom_mutex_free,
    ._mbedtls_mutex_lock = mbedtls_rom_mutex_lock,
    ._mbedtls_mutex_unlock = mbedtls_rom_mutex_unlock,
    ._mbedtls_calloc = MBEDTLS_PLATFORM_STD_CALLOC,
    ._mbedtls_free = MBEDTLS_PLATFORM_STD_FREE,

    /* Fill the SHA functions into mbedtls rom function table, since these functions are not exported in the ROM interface. */
    ._mbedtls_sha1_update = mbedtls_sha1_update,
    ._mbedtls_internal_sha1_process = mbedtls_internal_sha1_process,
    ._mbedtls_sha256_update = mbedtls_sha256_update,
    ._mbedtls_internal_sha256_process = mbedtls_internal_sha256_process,
};

__attribute__((constructor)) void mbedtls_rom_osi_functions_init(void)
{
    /* Initialize the pointer of mbedtls rom osi function table. */
    extern mbedtls_rom_funcs_t *mbedtls_rom_osi_funcs_ptr;
    mbedtls_rom_osi_funcs_ptr = (mbedtls_rom_funcs_t *)&mbedtls_rom_funcs_table;
}
